package com.erp.wallet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.wallet.api.domain.ErpSellerStoreWalletLog;
import com.erp.wallet.domain.bo.ErpSellerStoreWalletLogBo;
import com.erp.wallet.domain.vo.ErpSellerStoreWalletLogVo;
import com.erp.wallet.mapper.ErpSellerStoreWalletLogMapper;
import com.erp.wallet.service.IErpSellerStoreWalletLogService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * erp用户钱包日志Service业务层处理
 *
 * @author erp
 * @date 2023-05-09
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStoreWalletLogServiceImpl implements IErpSellerStoreWalletLogService {

    private final ErpSellerStoreWalletLogMapper baseMapper;

    /**
     * 查询erp用户钱包日志
     */
    @Override
    public ErpSellerStoreWalletLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询erp用户钱包日志列表
     */
    @Override
    public TableDataInfo<ErpSellerStoreWalletLogVo> queryPageList(ErpSellerStoreWalletLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerStoreWalletLog> lqw = buildQueryWrapper(bo);
        Page<ErpSellerStoreWalletLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询erp用户钱包日志列表
     */
    @Override
    public List<ErpSellerStoreWalletLogVo> queryList(ErpSellerStoreWalletLogBo bo) {
        LambdaQueryWrapper<ErpSellerStoreWalletLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerStoreWalletLog> buildQueryWrapper(ErpSellerStoreWalletLogBo bo) {
        LambdaQueryWrapper<ErpSellerStoreWalletLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSellerId() != null, ErpSellerStoreWalletLog::getSellerId, bo.getSellerId());
        lqw.eq(StringUtils.isNotBlank(bo.getSerialNumber()), ErpSellerStoreWalletLog::getSerialNumber, bo.getSerialNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNumber()), ErpSellerStoreWalletLog::getOrderNumber, bo.getOrderNumber());
        lqw.eq(bo.getFlowType() != null, ErpSellerStoreWalletLog::getFlowType, bo.getFlowType());
        lqw.eq(bo.getExpenseType() != null, ErpSellerStoreWalletLog::getExpenseType, bo.getExpenseType());
        lqw.eq(bo.getPayType() != null, ErpSellerStoreWalletLog::getPayType, bo.getPayType());
        lqw.eq(bo.getMoney() != null, ErpSellerStoreWalletLog::getMoney, bo.getMoney());
        lqw.eq(bo.getAddTime() != null, ErpSellerStoreWalletLog::getAddTime, bo.getAddTime());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerStoreWalletLog::getRevision, bo.getRevision());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), ErpSellerStoreWalletLog::getCreateBy, bo.getCreateBy());
        lqw.orderByDesc(ErpSellerStoreWalletLog::getAddTime);

        return lqw;
    }

    /**
     * 新增erp用户钱包日志
     */
    @Override
    public Boolean insertByBo(ErpSellerStoreWalletLogBo bo) {
        ErpSellerStoreWalletLog add = BeanUtil.toBean(bo, ErpSellerStoreWalletLog.class);
        validEntityBeforeSave(add);
        add.setAddTime(new Date());
        // 生成流水号
        String serialNumber = "PB" + System.currentTimeMillis();
        add.setSerialNumber(serialNumber);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改erp用户钱包日志
     */
    @Override
    public Boolean updateByBo(ErpSellerStoreWalletLogBo bo) {
        ErpSellerStoreWalletLog update = BeanUtil.toBean(bo, ErpSellerStoreWalletLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerStoreWalletLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除erp用户钱包日志
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public ErpSellerStoreWalletLog selectMoney(String orderId) {
        List<ErpSellerStoreWalletLog> walletLogList = baseMapper.selectList(new LambdaQueryWrapper<ErpSellerStoreWalletLog>()
                .eq(ErpSellerStoreWalletLog::getOrderNumber, orderId)
                .eq(ErpSellerStoreWalletLog::getFlowType,1)
                .eq(ErpSellerStoreWalletLog::getExpenseType, 2)
                .orderByDesc(ErpSellerStoreWalletLog::getAddTime));
        if (ObjectUtils.isNotEmpty(walletLogList)) {
            return walletLogList.get(0);
        }
        return null;
    }

    @Override
    public ErpSellerStoreWalletLog getWalletLogByOrderNumber(String orderNumber) {
        return baseMapper.selectOne(new LambdaQueryWrapper<ErpSellerStoreWalletLog>().eq(ErpSellerStoreWalletLog::getOrderNumber,orderNumber));
    }
}
